<template>
  <div class="staCompreWrap">
    <van-nav-bar class="top">
      <van-icon
        class="rights"
        v-if="deLete == false"
        name="plus"
        slot="right"
        @click="onClickRight"
      />
      <van-icon
        v-if="deLete == true"
        slot="right"
        class="rights"
        name="success"
        @click="deLete = false"
      />
      <van-icon
        name="arrow-left"
        v-if="deLete == false"
        slot="left"
        @click="onClickLeft"
        >地区管理</van-icon
      >
      <van-icon
        v-if="deLete == true"
        class="rights"
        name="cross"
        slot="left"
        @click="deLete = false"
        >编辑城市</van-icon
      >
    </van-nav-bar>

    <div
      class="label"
      v-for="(i, index) in data"
      :key="index"
      :class="
        i.wea == '晴朗' || i.wea == '晴朗/有风'
          ? 'qing'
          : i.wea == '大部多云' ||
            i.wea == '多云' ||
            i.wea == '局部多云' ||
            i.wea == '霾'
          ? 'duoyun'
          : i.wea == '雷阵雨'
          ? 'leizhenyu'
          : 'yu'
      "
      style="user-select: none"
      @touchstart="onTouchStart"
      @mousedown.prevent
      @mouseup.prevent
    >
      <!-- @click="onlabel(i)" -->
      <div
        style="
          width: 100%;
          height: 100%;
          margin-right: 50px;
          position: absolute;
        "
        @touchend="onTouchEnd(i)"
        :disabled="deLete"
      >
        <div
          class="left"
          v-if="
            i.wea == '局部多云' ||
            i.wea == '雨' ||
            i.wea == '大部多云' ||
            i.wea == '多云' ||
            i.wea == '小雨' ||
            i.wea == '中雨' ||
            i.wea == '大雨' ||
            i.wea == '霾' ||
            i.wea == '阵雨'
          "
        >
          <div
            style="
              font-size: 44px;

              font-weight: bold;
              color: #101010;
            "
          >
            {{ i.tem }}°
          </div>
          <div
            style="
              font-size: 12px;

              font-weight: 500;
              color: #101010;
              position: absolute;
              top: 26px;
              width: 100px;
              left: 50px;
            "
          >
            {{ i.wea }}
          </div>
          <div
            style="
              font-size: 13px;

              font-weight: 400;
              color: #656b75;
            "
          >
            {{ i.cityName }}{{ i.cnty }}
          </div>
        </div>
        <div
          :class="deLete == false ? 'right' : 'rightActive'"
          v-if="i.wea == '晴朗' || i.wea == '晴朗/有风'"
        >
          <div
            style="
              font-size: 44px;

              font-weight: bold;
              color: #101010;
            "
          >
            {{ i.tem }}°
          </div>
          <div
            style="
              font-size: 12px;

              font-weight: 500;
              color: #101010;
              position: absolute;
              width: 100px;
              top: 26px;
              left: 65px;
            "
          >
            {{ i.wea }}
          </div>
          <div
            style="
              font-size: 13px;

              font-weight: 400;
              color: #656b75;
            "
          >
            {{ i.cityName }}{{ i.cnty }}
          </div>
        </div>
        <div v-else class="left">
          <div
            style="
              font-size: 44px;

              font-weight: bold;
              color: #101010;
            "
          >
            {{ i.tem }}°
          </div>
          <div
            style="
              font-size: 12px;

              font-weight: 500;
              color: #101010;
              position: absolute;
              top: 26px;
              width: 100px;
              left: 50px;
            "
          >
            {{ i.wea }}
          </div>
          <div
            style="
              font-size: 13px;

              font-weight: 400;
              color: #656b75;
            "
          >
            {{ i.cityName }}{{ i.cnty }}
          </div>
        </div>
      </div>
      <div class="rights1" v-if="deLete">
        <van-icon name="delete-o" @click="handlerTouchstart(i.id)" />
      </div>
    </div>

    <comArea
      v-if="showArea"
      :add="add"
      @closeEmit="closeAreaMet"
      @confirmEmit="addCity"
    >
    </comArea>
    <!--最底部文字-->

    <div class="modal-value" v-if="showMoadl">
      <div class="modal-value0">确定删除该地区吗？</div>
      <div class="modal-value1" @click="pass()">确定</div>
      <div class="modal-value2" @click="nopass()">取消</div>
    </div>
  </div>
</template>

<script type="text/javascript">
import meteoroHomeApi from "@/service/regionalManagement.js";
import btnDetail from "@/components/staCompre/btnDetail.vue";
import comCity from "@/components/common/city.vue";
import area from "@/components/mixins/area.js";
import index from "@/router/index.js";
import Vue from "vue";
import { SwipeCell, Card } from "vant";
import { getItem } from "../util/storage";
Vue.use(SwipeCell);
Vue.use(Card);
export default {
  mixins: [area, index],
  data() {
    return {
      active: 0,
      tabActive: 0,
      city: "",
      showArea: false,
      loadingSuc: false,
      islongPress: false,
      showMoadl: false,
      id: 0,
      data: [],
      timeOutEvent: null,
      deLete: false,
    };
  },
  mounted() {
    this.initApi();
  },
  watch: {
    showArea: {
      immediate: true,
      handler(val, old) {
        if (this.showArea == true) {
          this.add = "添加";
        }
      },
    },
  },
  components: {
    btnDetail,
    comCity,
  },

  methods: {
    onlabel(i) {
      var areaSelectValue = [{}, { name: i.cityName }, { name: i.cnty }];
      var result = [i.longitude, i.latitude];
      this.$store.dispatch("setLocation", areaSelectValue);
      this.$store.dispatch("setCityLongAndLati", result);

      this.$router.push({
        name: "Home",
        params: {
          blon: true,
        },
      });
    },
    onTouchStart() {
      this.timeOutEvent = setTimeout(() => {
        this.longPress();
      }, 2000); // 5秒后开始触发 longPress 函数
    },
    longPress() {
      this.timeOutEvent = 0;
      //执行长按要执行的内容，如删除操作
      this.deLete = true;
    },
    onTouchEnd(i) {
      clearTimeout(this.timeOutEvent); //清除定时器
      if (this.timeOutEvent != 0) {
        //意味着长按时间不到规定时间，可以进行其他操作，例如 onCLick
        if (this.deLete == false) {
          // 在这里处理点击事件的逻辑
          this.onlabel(i);
        }
        this.deLete = false;
      }
    },
    handlerTouchstart(id) {
      this.id = id;
      this.showMoadl = true;
    },
    handlerTouchend(id) {
     
    },
    onClickLeft() {
      this.$router.push({
        path: "/Home",
        query: {
          name: "1",
        },
      });
    },

    pass() {
      console.log(this.id);
      var params = {
        id: this.id,
      };
      meteoroHomeApi
        .deleteCityWeather(params)
        .then((res) => {
          this.$toast.loading({
            message: "删除成功",
            forbidClick: true,
            duration: 0,
          });
          this.showMoadl = false;
          this.$toast.clear();
          this.initApi();
        })
        .catch((error) => {
          this.$toast.clear();
        });
    },
    nopass() {
      this.showMoadl = false;
    },
    onClickRight() {
      if (this.tabActive == 1) {
        this.$toast("此菜单下，请点击地图！");
        return;
      }
      this.showArea = true;
    },
    initApi() {
      var params = {
        userId: this.$store.getters.userid,
      };
      meteoroHomeApi
        .cityWeather(params)
        .then((res) => {
      
          this.data = res.data;
        
          this.$toast.clear();
        })

        .catch((error) => {
          this.$toast.clear();
        });
    },

    addCity(value) {
      console.log(this.data);
      console.log(value, "fsadff");
      var _this = this;
      //24小时最高/最低温度
      SGMap.plugin("SGMap.GeocodingTask").then(function (res) {
        var geocodingTask = new SGMap.GeocodingTask();
        geocodingTask
          .getLocation({
            address: value[1].name + value[2].name,
            region: "山西省",
            isEncode: false,
          })
          .then(function (result) {
            var lnglat = result.location;
            console.log(lnglat);
            var params = {
              userId: _this.$store.getters.userid,
              cityName: value[1].name,
              cnty: value[2].name,
              provinceName: "山西省",
              longitude: lnglat[0], //新增
              latitude: lnglat[1], //新增
            };
            meteoroHomeApi
              .addCityWeather(params)
              .then((res) => {
                console.log(res);
                if (res == "success") {
                  _this.$toast("添加成功!");
                } else {
                  _this.$toast(res);
                }
                _this.showArea = false;

                _this.initApi();
              })
              .catch((error) => {});
          });
      });
    },
    toPage(item) {
      this.$router.push("/");
    },
  },
};
</script>
<style type="text/css" lang="less" scoped>
body {
  background-color: #f3f7fa;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.goods-card {
  margin: 0;
  background-color: #fff;
}
/deep/ .van-swipe-cell__wrapper {
  margin-bottom: 10px;
}

.delete-button {
  height: 100%;
}
.lineRight {
  position: absolute;
  top: -30px;
  right: 10%;
  height: 16px;
  vertical-align: middle;
  margin-bottom: 2px;
}
.top {
  color: #686566;
}
/deep/ .van-nav-bar__text {
  color: #fff !important;
}
/deep/ .van-nav-bar .van-icon {
  color: #fff !important;
}
/deep/ .van-nav-bar {
  background: #357cff;
}
.staCompreWrap {
  font-size: 16px;
  .pageCover();
  position: absolute;
  height: inherit;
  background-color: #f3f7fa;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  height: auto;
  overflow-y: auto;

  .van-tabs {
    margin: 10px;
  }

  .van-tab--active {
    color: #1989fa;
  }

  .van-tabs--line .van-tabs__wrap {
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
  }
}

.factorBg {
  position: absolute;
  left: 0;
  .px2rem(bottom, 0px);
  width: 100%;
  height: 200px;
}

.label {
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 3%;

  left: 5%;
  width: 90%;
  height: 19%;
  margin-top: 5px;
  color: aliceblue;
  background-size: 100% 100%;
  .left {
    position: absolute;
    left: 90px;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .right {
    position: absolute;
    right: -70px;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 100px;
  }
  .rightActive {
    position: absolute;
    right: -30px;
    top: 50%;
    width: 100px;
    transform: translate(-50%, -50%);
  }
  .rights1 {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translate(-50%, -50%);
    /deep/.van-icon {
      font-size: 28px;
      color: black;
    }
  }
}

.leizhenyu {
  background-image: url("../assets/newhome/diqu/leizhenyu.png");
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.qing {
  background-image: url("../assets/newhome/diqu/qing.png");
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.duoyun {
  background-image: url("../assets/newhome/diqu/duoyun.png");
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.yu {
  background-image: url("../assets/newhome/diqu/yu.png");
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.label1 {
  position: relative;
  top: 10%;
  right: 0;
  height: 50%;
  width: 90%;
}

.label2 {
  display: inline-block;

  right: 65%;
  font-size: 150%;
}

.label3 {
  display: inline-block;
  font-size: 150%;
  position: absolute;
  right: 10%;
}

.bottomCon0 {
  position: fixed;
  .px2rem(width, 60px);
  .px2rem(height, 60px);
  bottom: 2%;

  right: 10%;
  background-image: url("../assets/regionalManagement/add.png");

  background-size: 100% 100%;
}

.del-model {
  .el-message-Box__btns {
    .el-button:nth-child(1) {
      float: right;
    }

    .el-button:nth-child(2) {
      margin-right: 10px;
      background-color: #2d8cf0;
      border-color: #2d8cf0;
    }
  }
}
.modal-value {
  position: fixed;
  width: 80%;

  height: 20%;
  bottom: 40%;

  right: 10%;
  overflow: hidden;
  background-color: #fff;
  border-radius: 12px;
  background-size: 100% 100%;
}

.modal-value0 {
  text-align: center;
  position: absolute;
  height: 60%;
  width: 100%;
  background-color: #fff;
  font-size: 25px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.modal-value1 {
  text-align: center;
  position: absolute;
  top: 60%;
  height: 40%;
  width: 50%;
  background-color: rgb(194, 150, 94);
  font-size: 25px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.modal-value2 {
  text-align: center;
  position: absolute;
  top: 60%;
  left: 50%;
  height: 40%;
  width: 50%;
  background-color: rgb(148, 193, 94);
  font-size: 25px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}
</style>
